package com.lsa.other;

import java.util.ArrayDeque;
import java.util.Stack;
import java.util.StringTokenizer;

public class PathAbsolutizer
{
	public static String getAbsolutePath(String path)
	{
		ArrayDeque<String> pathStack = new ArrayDeque<String>();
		
		final String pathSeparator = System.getProperty("file.separator");
		
		StringTokenizer st = new StringTokenizer(path, pathSeparator);
		while(st.hasMoreTokens())
		{
			String part = st.nextToken();
			if (part.equals("."))
			{
				continue;
			}
			else if (part.equals(".."))
			{
				pathStack.pop();
			}
			else
			{
				pathStack.push(part);
			}
		}
		
		StringBuffer absPath = new StringBuffer();
		
		while(!pathStack.isEmpty())
		{
			absPath.append(pathSeparator).append(pathStack.pollLast());
		}
		
		return absPath.toString();
	}
}
